//
//  KRLInteractionBar.m
//  KaraLive
//
//  Created by Zhijia Zhong on 2018/9/15.
//  Copyright © 2018年 naxigoren. All rights reserved.
//

#import "KRLInteractionBar.h"

@interface KRLInteractionBar()

@property (nonatomic, strong) UIButton *likeButton;
@property (nonatomic, strong) UILabel *likesLabel;
@property (nonatomic, strong) UIButton *commentButton;
@property (nonatomic, strong) UILabel *commentsLabel;
@property (nonatomic, strong) UIButton *shareButton;
@property (nonatomic, strong) UILabel *sharesLabel;

@property (nonatomic, strong) UIImageView *musicIcon;
@property (nonatomic, strong) UILabel *musicLabel;

@end

@implementation KRLInteractionBar

@dynamic likes;
@dynamic comments;
@dynamic musicName;
@dynamic showMusic;

- (UIButton *)likeButton {
    if (!_likeButton) {
        _likeButton = [[UIButton alloc] init];
        [_likeButton setImage:[UIImage imageNamed:@"icon_praise"] forState:UIControlStateNormal];
        [_likeButton setImage:[UIImage imageNamed:@"icon_praise_pre"] forState:UIControlStateSelected];
    }
    return _likeButton;
}

- (UILabel *)likesLabel {
    if (!_likesLabel) {
        _likesLabel = [[UILabel alloc] init];
        _likesLabel.textColor = [UIColor whiteColor];
        _likesLabel.font = [UIFont systemFontOfSize:10 weight:UIFontWeightMedium];
    }
    return _likesLabel;
}

- (UIButton *)commentButton {
    if (!_commentButton) {
        _commentButton = [[UIButton alloc] init];
        [_commentButton setImage:[UIImage imageNamed:@"icon_discuss"] forState:UIControlStateNormal];
    }
    return _commentButton;
}

- (UILabel *)commentsLabel {
    if (!_commentsLabel) {
        _commentsLabel = [[UILabel alloc] init];
        _commentsLabel.textColor = [UIColor whiteColor];
        _commentsLabel.font = [UIFont systemFontOfSize:10 weight:UIFontWeightMedium];
    }
    return _commentsLabel;
}

- (UIButton *)shareButton {
    if (!_shareButton) {
        _shareButton = [[UIButton alloc] init];
        [_shareButton setImage:[UIImage imageNamed:@"icon_share"] forState:UIControlStateNormal];
    }
    return _shareButton;
}

- (UILabel *)sharesLabel {
    if (!_sharesLabel) {
        _sharesLabel = [[UILabel alloc] init];
        _sharesLabel.textColor = [UIColor whiteColor];
        _sharesLabel.font = [UIFont systemFontOfSize:10 weight:UIFontWeightMedium];
    }
    return _sharesLabel;
}

- (UIImageView *)musicIcon {
    if (!_musicIcon) {
        _musicIcon = [[UIImageView alloc] init];
        _musicIcon.image = [UIImage imageNamed:@"ico_timeline_music"];
    }
    return _musicIcon;
}

- (UILabel *)musicLabel {
    if (!_musicLabel) {
        _musicLabel = [[UILabel alloc] init];
        _musicLabel.textColor = [UIColor whiteColor];
        _musicLabel.font = [UIFont systemFontOfSize:10 weight:UIFontWeightMedium];
    }
    return _musicLabel;
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.translatesAutoresizingMaskIntoConstraints = NO;
        
        self.colors = @[[UIColor colorWithRGB:0x000000 alpha:0], [UIColor blackColor]];
        self.startPoint = CGPointMake(0, 0);
        self.endPoint = CGPointMake(0, 1);
        
        [self addSubview:self.likeButton];
        [self addSubview:self.likesLabel];
        [self addSubview:self.commentButton];
        [self addSubview:self.commentsLabel];
        [self addSubview:self.shareButton];
    }
    return self;
}


+ (BOOL)requiresConstraintBasedLayout {
    return YES;
}

- (void)updateConstraints {
    [self.likeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(16);
        make.centerY.mas_equalTo(0);
        make.size.mas_equalTo(CGSizeMake(15, 15));
    }];
    
    [self.likesLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.likeButton.mas_right).offset(4.5);
        make.centerY.mas_equalTo(0);
    }];

    [self.commentButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.likeButton.mas_right).offset(42);
        make.centerY.mas_equalTo(0);
        make.size.mas_equalTo(CGSizeMake(15, 15));
    }];
    
    [self.commentsLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.commentButton.mas_right).offset(4.5);
        make.centerY.mas_equalTo(0);
    }];

    [self.shareButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.commentButton.mas_right).offset(42);
        make.centerY.mas_equalTo(0);
        make.size.mas_equalTo(CGSizeMake(16, 16));
    }];

    [super updateConstraints];
}

- (void)setLikes:(NSInteger)likes {
    self.likesLabel.text = [NSString stringWithFormat:@"%ld", likes];
}

- (void)setComments:(NSInteger)comments {
    self.commentsLabel.text = [NSString stringWithFormat:@"%ld", comments];
}

- (void)setMusicName:(NSString *)musicName {
    self.musicLabel.text = musicName;
}

- (void)setShowMusic:(BOOL)showMusic {
    if (showMusic) {
        [self addSubview:self.musicLabel];
        [self addSubview:self.musicIcon];
        
        [self.musicLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(-16);
            make.centerY.mas_equalTo(0);
        }];
        
        [self.musicIcon mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(0);
            make.size.mas_equalTo(CGSizeMake(16, 16));
            make.right.mas_equalTo(self.musicLabel.mas_left).offset(-4);
        }];
    } else {
        [self.musicIcon removeFromSuperview];
        [self.musicLabel removeFromSuperview];
    }
}

@end
